package com.regret.medicine.type;

import com.regret.medicine.BinlogParser;

import java.sql.Date;
import java.sql.JDBCType;
import java.util.Objects;

public class Column {

    private String columnName;
    private JDBCType jdbcType;

    public Column() {
    }

    public Column(String columnName, JDBCType jdbcType) {
        this.columnName = columnName;
        this.jdbcType = jdbcType;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public JDBCType getJdbcType() {
        return jdbcType;
    }

    public void setJdbcType(JDBCType jdbcType) {
        this.jdbcType = jdbcType;
    }

    public String getStringData(Object value) {

        if (Objects.isNull(value)) {
            return null;
        }
        String result = null;

        switch (jdbcType) {
            case BIT:
            case TINYINT:
            case SMALLINT:
            case INTEGER:
            case BIGINT:
            case FLOAT:
            case DOUBLE:
            case NUMERIC:
            case DECIMAL:
            case CHAR:
            case VARCHAR:
            case LONGVARCHAR:
                result = String.valueOf(value);
                break;
            case DATE:
            case TIME:
            case TIMESTAMP:
            case TIMESTAMP_WITH_TIMEZONE:
                if (value instanceof Long) {
                    result = BinlogParser.simpleDateFormat.format(new java.util.Date((Long) value));
                } else {
                    result = BinlogParser.simpleDateFormat.format((Date) value);
                }
                break;
        }

        return result;
    }
}
